# -*- coding:utf-8 -*-

from pydantic import BaseModel, Field, validator
from typing import Optional, List, Dict, Any
from datetime import datetime


class ServerHostModel(BaseModel):
    """服务器主机基础模型"""
    id: Optional[int] = Field(default=None, description='主机ID')
    host_name: str = Field(..., description='主机名称')
    host_ip: str = Field(..., description='主机IP地址')
    internal_ip: Optional[str] = Field(default=None, description='内网IP地址（优先使用）')
    internal_ssh_port: Optional[int] = Field(default=None, description='内网SSH端口（为空则使用ssh_port）')
    ssh_port: int = Field(default=22, description='SSH端口')
    ssh_username: str = Field(..., description='SSH用户名')
    ssh_password: Optional[str] = Field(default=None, description='SSH密码')
    agent_path: str = Field(default='/home/project/', description='Agent项目路径')
    agent_status: Optional[str] = Field(default='unknown', description='Agent状态')
    agent_version: Optional[str] = Field(default=None, description='Agent版本号')
    deploy_status: Optional[str] = Field(default='not_deployed', description='部署状态')
    deploy_error: Optional[str] = Field(default=None, description='部署错误信息')
    connection_status: Optional[str] = Field(default='disconnected', description='连接状态')
    latency: Optional[int] = Field(default=None, description='连接延迟（毫秒）')
    connection_type: Optional[str] = Field(default=None, description='连接类型（内网/外网/外网（降级））')
    last_connect_time: Optional[datetime] = Field(default=None, description='最后连接时间')
    last_deploy_time: Optional[datetime] = Field(default=None, description='最后部署时间')
    remark: Optional[str] = Field(default=None, description='备注')
    status: str = Field(default='0', description='状态（0正常 1停用）')
    create_time: Optional[datetime] = Field(default=None, description='创建时间')
    update_time: Optional[datetime] = Field(default=None, description='更新时间')

    class Config:
        from_attributes = True


class ServerHostQueryModel(BaseModel):
    """服务器主机查询模型"""
    host_name: Optional[str] = Field(default=None, description='主机名称')
    host_ip: Optional[str] = Field(default=None, description='主机IP地址')
    agent_status: Optional[str] = Field(default=None, description='Agent状态')
    connection_status: Optional[str] = Field(default=None, description='连接状态')
    status: Optional[str] = Field(default=None, description='状态')


class ServerHostCreateModel(BaseModel):
    """服务器主机创建模型"""
    host_name: str = Field(..., description='主机名称', min_length=1, max_length=100)
    host_ip: str = Field(..., description='主机IP地址', min_length=1, max_length=50)
    internal_ip: Optional[str] = Field(default=None, description='内网IP地址（优先使用）', max_length=50)
    internal_ssh_port: Optional[int] = Field(default=None, description='内网SSH端口（为空则使用ssh_port）', ge=1, le=65535)
    ssh_port: int = Field(default=22, description='SSH端口', ge=1, le=65535)
    ssh_username: str = Field(..., description='SSH用户名', min_length=1, max_length=50)
    ssh_password: str = Field(..., description='SSH密码', min_length=1, max_length=200)
    agent_path: str = Field(default='/home/project/', description='Agent项目路径')
    remark: Optional[str] = Field(default=None, description='备注', max_length=500)
    status: str = Field(default='0', description='状态（0正常 1停用）')


class ServerHostUpdateModel(BaseModel):
    """服务器主机更新模型"""
    host_name: Optional[str] = Field(default=None, description='主机名称', max_length=100)
    host_ip: Optional[str] = Field(default=None, description='主机IP地址', max_length=50)
    internal_ip: Optional[str] = Field(default=None, description='内网IP地址', max_length=50)
    internal_ssh_port: Optional[int] = Field(default=None, description='内网SSH端口', ge=1, le=65535)
    ssh_port: Optional[int] = Field(default=None, description='SSH端口', ge=1, le=65535)
    ssh_username: Optional[str] = Field(default=None, description='SSH用户名', max_length=50)
    ssh_password: Optional[str] = Field(default=None, description='SSH密码', max_length=200)
    agent_path: Optional[str] = Field(default=None, description='Agent项目路径')
    agent_status: Optional[str] = Field(default=None, description='Agent状态')
    agent_version: Optional[str] = Field(default=None, description='Agent版本号')
    deploy_status: Optional[str] = Field(default=None, description='部署状态')
    deploy_error: Optional[str] = Field(default=None, description='部署错误信息')
    connection_status: Optional[str] = Field(default=None, description='连接状态')
    remark: Optional[str] = Field(default=None, description='备注', max_length=500)
    status: Optional[str] = Field(default=None, description='状态（0正常 1停用）')


class ServerHostBatchImportModel(BaseModel):
    """服务器主机批量导入模型"""
    ip_list: List[str] = Field(..., description='IP地址列表')
    ssh_port: int = Field(default=22, description='SSH端口', ge=1, le=65535)
    ssh_username: str = Field(..., description='SSH用户名', min_length=1, max_length=50)
    ssh_password: str = Field(..., description='SSH密码', min_length=1, max_length=200)
    agent_path: str = Field(default='/home/project/', description='Agent项目路径')
    name_prefix: str = Field(default='Server', description='主机名称前缀')


class CommandExecuteModel(BaseModel):
    """命令执行模型"""
    host_ids: List[int] = Field(..., description='主机ID列表')
    command: str = Field(..., description='执行的命令', min_length=1)
    command_type: str = Field(default='custom', description='命令类型')


class CommandExecuteResultModel(BaseModel):
    """命令执行结果模型"""
    host_id: int = Field(..., description='主机ID')
    host_ip: str = Field(..., description='主机IP')
    host_name: str = Field(..., description='主机名称')
    status: str = Field(..., description='执行状态: success/failed')
    result: Optional[str] = Field(default=None, description='执行结果')
    error: Optional[str] = Field(default=None, description='错误信息')
    duration: Optional[int] = Field(default=None, description='执行耗时（毫秒）')


class AgentDeployModel(BaseModel):
    """Agent部署模型"""
    host_ids: List[int] = Field(..., description='主机ID列表')
    agent_file_path: str = Field(..., description='Agent文件下载URL（或服务器本地路径，兼容旧方式）')


class AgentControlModel(BaseModel):
    """Agent控制模型"""
    host_ids: List[int] = Field(..., description='主机ID列表')
    action: str = Field(..., description='操作: start/stop/restart')


class AgentLogQueryModel(BaseModel):
    """Agent日志查询模型"""
    host_id: int = Field(..., description='主机ID')
    log_date: Optional[str] = Field(default=None, description='日志日期(YYYY-MM-DD)')
    tail_lines: Optional[int] = Field(default=100, description='读取最后N行')


class ServerCommandLogModel(BaseModel):
    """服务器命令日志模型"""
    id: Optional[int] = Field(default=None, description='日志ID')
    host_id: Optional[int] = Field(default=None, description='主机ID')
    host_ip: Optional[str] = Field(default=None, description='主机IP')
    command: str = Field(..., description='执行的命令')
    command_type: str = Field(default='custom', description='命令类型')
    result: Optional[str] = Field(default=None, description='执行结果')
    error: Optional[str] = Field(default=None, description='错误信息')
    status: str = Field(..., description='执行状态')
    execute_time: Optional[datetime] = Field(default=None, description='执行时间')
    duration: Optional[int] = Field(default=None, description='执行耗时（毫秒）')
    create_time: Optional[datetime] = Field(default=None, description='创建时间')

    class Config:
        from_attributes = True


class BatchTestConnectionModel(BaseModel):
    """批量测试连接请求模型"""
    host_ids: List[int] = Field(..., description='主机ID列表')

